package com.sam.jifenbao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sam.jifenbao.CouponUsedActivity;
import com.sam.jifenbao.R;

public class RedeemDialog extends Dialog{
    private TextView redeemCostTV,confirmBtn,cancelBtn,errorMessageTV;
    private EditText verifyCodeET;
    private Context context;

    public RedeemDialog(Context context) {
        super(context,android.R.style.Theme_Dialog);  // 创建对话框，并且应用Theme_Dialog这个主题设置对话框的一些属性
        this.context = context;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);   // 让对话框没有标题栏，即去掉标题栏
        setContentView(R.layout.dialog_redeem_points);
        // 把对话框的背景设置为透明的，那么我们设置什么layout上去，就显示什么样，不会有额外的背景
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        findView();
        initView();
    }

    private void findView(){
        redeemCostTV = (TextView) findViewById(R.id.redeem_cost);
        confirmBtn = (TextView) findViewById(R.id.confirm_btn);
        cancelBtn = (TextView) findViewById(R.id.cancel_btn);
        verifyCodeET = (EditText) findViewById(R.id.verify_code_et);
        errorMessageTV = (TextView) findViewById(R.id.error_message);
    }

    private void initView(){
        confirmBtn.setOnClickListener(onClickListener);
        cancelBtn.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.confirm_btn:
                    String code = verifyCodeET.getText().toString();  // 从EditText控件里获取输入的内容
                    if(TextUtils.isEmpty(code)) {  // isEmpty 判断字符串是否为null或者长度为0
                        verifyCodeET.requestFocus();
                        errorMessageTV.setVisibility(View.GONE);
                    } else if(isValidVerifyCode(code)){
                        errorMessageTV.setVisibility(View.GONE);
                        context.startActivity(new Intent(context, CouponUsedActivity.class));
                        dismiss(); //  关闭对话框的方法
                        if(onCouponUseSccueededLinstener != null) onCouponUseSccueededLinstener.onCouponUseSeccueeded();
                    } else {
                        errorMessageTV.setVisibility(View.VISIBLE);
                        verifyCodeET.requestFocus();
                    }
                    break;
                case R.id.cancel_btn:
                    Toast.makeText(context,"取消对兑换",Toast.LENGTH_SHORT).show();
                    dismiss(); //  关闭对话框的方法
                    break;
            }
        }
    };

    private boolean isValidVerifyCode(String code){
        int value = Integer.parseInt(code);
        return value % 10 + value / 10 % 10 + value / 100 % 10 + value / 1000 % 10 == 20;
    }

    private OnCouponUseSccueededLinstener onCouponUseSccueededLinstener;

    public void setOnCouponUseSccueededLinstener(OnCouponUseSccueededLinstener onCouponUseSccueededLinstener) {
        this.onCouponUseSccueededLinstener = onCouponUseSccueededLinstener;
    }
    public interface OnCouponUseSccueededLinstener{
        void onCouponUseSeccueeded();
    }

}
